
//
//  LYSettingchangeMobileController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/24.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSettingchangeMobileController.h"
#import "LYSendCodeButton.h"
#import "CountryCodeViewController.h"

@interface LYSettingchangeMobileController ()
{
//    UIButton *countryCodeBtn;
    
    NSString *countryCodeStr;
}
@property (nonatomic, strong) UITextField *mobileTF;
@property (nonatomic, strong) UITextField *codeTF;

@property (nonatomic, strong) LYSendCodeButton *sendCodeBtn;

@end

@implementation LYSettingchangeMobileController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改手机号";
    
//    countryCodeStr = UD_GetOForK(isCountryCode)?UD_GetOForK(isCountryCode):@"86";
    countryCodeStr = @"";
    [self createSubview];
}
- (void)countryCodeBtnTap {
    CountryCodeViewController *CCVC = [[CountryCodeViewController alloc] init];
    CCVC.selectCode = ^(NSString * _Nonnull code) {
        self->countryCodeStr = code;
//        [self->countryCodeBtn setTitle:[NSString stringWithFormat:@"+ %@", code] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:CCVC animated:YES];
}
- (void)createSubview{
    UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, WIDTH - 20, 25)];
    topLabel.textColor = THEMECOLOR_OF_TEXT_333333;
    topLabel.font = Font(Font_13);
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:topLabel];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"你当前绑定的手机号为"];
    NSAttributedString *strq = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" +%@ %@", countryCodeStr, self.mobile] attributes:@{NSForegroundColorAttributeName :THEMECOLOR_OF_ORANGE, NSFontAttributeName:Font(Font_13)}];
    [str appendAttributedString:strq];
    topLabel.attributedText = str;
    
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, topLabel.bottom, WIDTH - 20, 20)];
    messageLabel.textColor = THEMECOLOR_OF_TEXT_9a9a9a;
    messageLabel.font = [UIFont systemFontOfSize:11];
    messageLabel.text = @"修改后您登录的手机号将同步修改";
    messageLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:messageLabel];
    
//    countryCodeBtn = [UIToolsControl initButtonWithFrame:CGRectMake(15, messageLabel.bottom+15, 50, 45) imageName:nil target:self action:@selector(countryCodeBtnTap) title:[NSString stringWithFormat:@"+ %@", countryCodeStr] titleColor:THEMECOLOR_OF_TEXT_333333];
//    countryCodeBtn.titleLabel.font = Font(Font_15);
//    countryCodeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
//    countryCodeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [self.view addSubview:countryCodeBtn];
    
//    self.mobileTF = [[UITextField alloc] initWithFrame:CGRectMake(EndX(countryCodeBtn)+10, messageLabel.bottom+15, WIDTH - 30-60, 45)];
    self.mobileTF = [[UITextField alloc] initWithFrame:CGRectMake(15, messageLabel.bottom+15, WIDTH - 30-60, 45)];
    self.mobileTF.placeholder = @"请输入手机号";
    self.mobileTF.textColor = THEMECOLOR_OF_TEXT_333333;
    self.mobileTF.font = Font(Font_14);
    self.mobileTF.keyboardType = UIKeyboardTypeNumberPad;
    self.mobileTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.mobileTF];
    
    UIView *mobileline = [[UIView alloc] initWithFrame:CGRectMake(0, self.mobileTF.bottom, WIDTH, lineH)];
    mobileline.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
    [self.view addSubview:mobileline];
    
    _sendCodeBtn = [LYSendCodeButton buttonWithType:UIButtonTypeCustom];
    _sendCodeBtn.frame = CGRectMake(WIDTH - 100,mobileline.bottom + 7.5, 86, 30);
    [_sendCodeBtn addTarget:self action:@selector(getVerMobile:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_sendCodeBtn];
    
    self.codeTF = [[UITextField alloc] initWithFrame:CGRectMake(15, mobileline.bottom, _sendCodeBtn.left - 20, 45)];
    self.codeTF.placeholder = @"请输入验证码";
    self.codeTF.textColor = THEMECOLOR_OF_TEXT_333333;
    self.codeTF.font = Font(Font_14);
    self.codeTF.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.codeTF];
    
    UIView *codeline = [[UIView alloc] initWithFrame:CGRectMake(0, self.codeTF.bottom, WIDTH, lineH)];
    codeline.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
    [self.view addSubview:codeline];
    
    UIButton *surebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    surebutton.frame = CGRectMake( 40, codeline.bottom + 60, WIDTH-80, 40);
    surebutton.layer.cornerRadius = surebutton.height/2;
    surebutton.clipsToBounds = YES;
    surebutton.backgroundColor = THEMECOLOR_OF_ORANGE;
    [surebutton setTitle:@"确定" forState:UIControlStateNormal];
    [surebutton setTitleColor:COLOR_OF_WHITE forState:UIControlStateNormal];
    surebutton.titleLabel.font = Font(Font_14);
    [surebutton addTarget:self action:@selector(surebuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:surebutton];
}

#pragma mark -- Action
- (void)surebuttonClick:(UIButton *)button{
    //确定
    NSString *mobile = self.mobileTF.text;
    NSString *code = self.codeTF.text;
    if (mobile.length == 0) {
        //手机号不能为空
        SVPHUDError(@"请输入手机号");
        [self.mobileTF becomeFirstResponder];
        return;
    }
    if (UD_GetOForK(isMobile) && [UD_GetOForK(isMobile) isEqualToString:self.mobileTF.text]) {
        SVPHUDError(@"换绑手机号不能是登录手机号");
        [self.mobileTF becomeFirstResponder];
        return;
    }
    
    if (code.length == 0) {
        //验证码不能为空
        SVPHUDError(@"请输入验证码");
        [self.codeTF becomeFirstResponder];
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
//    dic[@"country_code"] = @"86";
    dic[@"mobile"] = mobile;
    dic[@"verify_code"] = code;
//    [dic setObject:countryCodeStr forKey:@"country_code"];
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Setting_bindMobile_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            //绑定成功
            UD_SetOForK(mobile, isMobile);
            weakSelf.block();
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

- (void)getVerMobile:(LYSendCodeButton *)codeBtn{
    [self.view endEditing:YES];
    NSString *mobile = self.mobileTF.text;
    
    if (mobile.length == 0) {
        //手机号不能为空
        SVPHUDError(@"手机号不能为空");
        [self.mobileTF becomeFirstResponder];
        return;
    }
    
    if(![UIToolsControl validatePhoneNumber:mobile]){
        //电话号码格式不对
        SVPHUDError(@"电话号码格式不对");
        [self.mobileTF becomeFirstResponder];
        return;
    }
    
//    if (mobile == self.mobile) {
//        //新的手机号和之前一致
//        return;
//    }
    if (UD_GetOForK(isMobile) && [UD_GetOForK(isMobile) isEqualToString:self.mobileTF.text]) {
        SVPHUDError(@"换绑手机号不能是登录手机号");
        [self.mobileTF becomeFirstResponder];
        return;
    }
    
   
    //校验新手机号是否绑定过
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:MyBaseInfo_Get_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            [codeBtn timeFailBeginFrom:60];
            [weakSelf requestCode:mobile];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

- (void)requestCode:(NSString *)mobile{
    SVPHUDShow;
    [UIToolsControl getCaptcha:mobile countryCode:countryCodeStr type:[UIToolsControl getCaptchaType:GetCaptchaTypeOfChangeAccount] successBlock:^(id response) {
        if ([response[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(response[@"msg"]);
        } else {
            SVPHUDError(response[@"msg"]);
        }
    }];
}

@end
